<template>
  <div class="mineFollowItem" v-if="userinfo" @click="toUser">
    <div class="left">
      <div class="avatar">
        <van-image
          round
          lazy-load
          width="50px"
          height="50px"
          :src="userinfo.avatar"
        />
      </div>
      <div class="userinfo">
        <div class="username">{{ userinfo.username }}</div>
        <div class="sub">
          {{ userinfo.occupation ? `${userinfo.occupation}&nbsp;·&nbsp;` : "" }}
          {{
            userinfo.ip_location ? `${userinfo.ip_location}&nbsp;·&nbsp;` : ""
          }}
          {{
            userinfo.count.count_follower
              ? `粉丝&nbsp;${userinfo.count.count_follower}`
              : ""
          }}
        </div>
      </div>
    </div>
    <div
      v-if="isFollow && this.followId != 13525021"
      class="unfollow"
      @click.stop="unfollow"
    >
      已关注
    </div>
    <div
      v-else-if="this.followId != 13525021"
      class="follow"
      @click.stop="follow"
    >
      关注
    </div>
  </div>
</template>

<script>
export default {
  props: ["followId"],
  data() {
    return {
      userinfo: null,
      isFollow: false,
    };
  },
  created() {
    // if (this.$route.query.userid == 13525021) {
    this.$toast.loading({
      message: "加载中...",
      forbidClick: true,
      duration: 0,
    });
    this.$axios
      .get(`https://apis.netstart.cn/xpc/user/${this.followId}`)
      .then((res) => {
        // console.log(res.data.data);
        this.userinfo = res.data.data;
        this.$toast.clear();
      });
    // }
  },
  mounted() {
    let follows = JSON.parse(localStorage.getItem("follows") || "[]");
    // console.log(follows.indexOf(this.article.author.userinfo.id));
    if (follows.indexOf(this.followId) != -1) {
      this.isFollow = true;
    }
  },
  methods: {
    unfollow() {
      this.$dialog
        .confirm({
          message: "确定要取消关注TA吗？",
        })
        .then(() => {
          // on confirm
          let follows = JSON.parse(
            localStorage.getItem("follows") || "[]"
          ).filter((o) => o != this.followId);
          localStorage.setItem("follows", JSON.stringify(follows));
          this.isFollow = false;
          this.$toast("取消关注成功");
        })
        .catch(() => {
          // on cancel
        });
    },
    follow() {
      let follows = JSON.parse(localStorage.getItem("follows") || "[]");
      follows.unshift(this.followId);
      localStorage.setItem("follows", JSON.stringify(follows));
      this.isFollow = true;
      this.$toast("关注成功");
    },
    toUser() {
      this.$router.push({ path: "/user", query: { userid: this.followId } });
    },
  },
};
</script>

<style lang="scss" scoped>
.mineFollowItem {
  border-bottom: 1px solid #f2f2f2;
  padding: 15px;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .left {
    display: flex;
    align-items: center;

    .avatar {
      width: 52px;
      height: 52px;
      border-radius: 50%;
      border: 1px solid #f2f2f2;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 8px;
    }

    .userinfo {
      display: flex;
      flex-direction: column;

      .username {
        font-size: 15px;
        font-weight: bold;
        margin-bottom: 3px;
      }

      .sub {
        font-size: 13px;
        color: #999;
        height: 18px;
        line-height: 18px;
      }
    }
  }

  .follow {
    width: 64px;
    text-align: center;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    font-size: 15px;
    background-color: #fdefe9;
    color: red;
  }

  .unfollow {
    width: 64px;
    text-align: center;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    font-size: 14px;
    background-color: #f7f7f7;
    color: #999;
  }
}
</style>
